/*	Copyright 2008 Jacquemin Valentin

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
	
	@author pollux
 */
package org.poxd.ui;

import java.awt.Component;
import java.awt.GridLayout;

import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;

import org.poxd.model.Language;

/**
 * @author pollux
 * 
 */
public class LanguageCellRenderer extends DefaultListCellRenderer implements
		ListCellRenderer {
	private static final long serialVersionUID = 1L;

	public static final int CELL_MARGIN_TOP = 4;

	public static final int CELL_MARGIN_BOTTOM = 4;

	public static final int CELL_MARGIN_LEFT = 0;

	public static final int CELL_MARGIN_RIGHT = 1;

	private static final float LANGUAGE_LABEL_FONT_SIZE = 10f;

	/*
	 * @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList,
	 *      java.lang.Object, int, boolean, boolean)
	 */
	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		JLabel label = (JLabel) super.getListCellRendererComponent(list, value,
				index, isSelected, cellHasFocus);
		Language language = (Language) value;
		String text = language.getName();
		// TODO not in EDT
		Icon icon = IconManager.getInstance().getIcon(language.getIcon());
		label.setIcon(icon);
		label.setFont(label.getFont().deriveFont(LANGUAGE_LABEL_FONT_SIZE));
		label.setText(text);
		label.setHorizontalTextPosition(SwingConstants.CENTER);
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setVerticalTextPosition(SwingConstants.BOTTOM);
		label.setAlignmentY(CENTER_ALIGNMENT);
		int iconHeight = label.getIcon().getIconHeight();
		label.setIconTextGap(32 - iconHeight + 1);

		JPanel ret = new JPanel(new GridLayout(1,1));
		ret.add(label);
		ret.setBackground(list.getBackground());
		return ret;
	}

}
